package com.study.fetch.ths;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONReader.Feature;
import com.study.fetch.ths.ZfDemo.ClsRootDTO.ClsZtDTO;
import com.study.get.jsl.BeileizhenDemo.TenTool;
import lombok.Data;
import lombok.experimental.Accessors;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhuzi
 * 2025/2/21 下午12:13
 */
public class ZfDemo {

    @Test
    public void zf() {
        String url = "https://x-quote.cls.cn/v2/quote/a/plate/up_down_analysis";

        String block = TenTool.webClient.get().uri(url)
                .retrieve().bodyToMono(String.class).timeout(Duration.ofSeconds(8))
                .retry(3).block();

        JSONObject jsonObject = JSON.parseObject(block);
        String jsonStr = jsonObject.getJSONObject("data").getString("plate_stock");
        List<ClsZtDTO> clsZtDTOS = JSON.parseArray(jsonStr, ClsZtDTO.class, Feature.SupportSmartMatch);
        for (ClsZtDTO clsZtDTO : clsZtDTOS) {
            System.out.println(clsZtDTO);
        }

    }

    public interface ClsRootDTO {

        /**
         * 数据来自 https://x-quote.cls.cn/v2/quote/a/plate/up_down_analysis?sign=71938a28aa2430f3c80cad2715e8e612
         */
        @Data
        @Accessors(chain = true)
        class ClsZtDTO {
            private String secuCode;
            private String secuName;
            private double change;
            private String upReason;
            private int plateStockUpNum;
            private List<ClsZtStock> stock_list = new ArrayList<>();
        }

        @Data
        @Accessors(chain = true)
        class ClsZtStock {
            private String secuCode;
            private String secuName;
            private BigDecimal change;
            private BigDecimal lastPx;
            private long cmc;
            private LocalDateTime time;
            private String upNum;
            private String upReason;
            private List<String> upTags;

            public ClsZtStock setSecuCode(String secuCode) {
                this.secuCode = StrUtil.sub(secuCode, 2, 8);
                return this;
            }
        }
    }
}
